package org.jsola.hr.web;

import org.jsola.core.Page;
import org.jsola.core.Result;
import org.jsola.exception.ParamException;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dto.EmployeeForSalaryDTO;
import org.jsola.hr.dto.SalaryPlanAddDTO;
import org.jsola.hr.dto.SalaryPlanBatchDTO;
import org.jsola.hr.dto.SalaryPlanUpdateDTO;
import org.jsola.hr.query.SalaryPlanQuery;
import org.jsola.hr.service.ISalaryPlanService;
import org.jsola.hr.vo.SalaryPlanListVO;
import org.jsola.hr.vo.SalaryPlanVO;
import org.jsola.user.core.TokenUser;
import org.jsola.user.core.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import javax.validation.Valid;


/**
 * @author zhr
 */
@RestController("hrSalaryPlanController")
@RequestMapping(HrConstants.API_PATH + "/v1/salaryPlans")
public class SalaryPlanController implements ISalaryPlanControllerAPI {

    @Autowired
    private ISalaryPlanService salaryPlanService;

    @Override
    @PostMapping
    public Result<SalaryPlanVO> save(@Valid @RequestBody SalaryPlanAddDTO salaryPlanAddDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 新增工资方案权限校验

        return Result.success(
                salaryPlanService.save(salaryPlanAddDTO, tokenUser)
        );
    }

    @Override
    @PostMapping(value = "/add")
    public Result<SalaryPlanVO> add(@Valid @RequestBody SalaryPlanAddDTO salaryPlanAddDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 新增工资方案权限校验

        return Result.success(
                salaryPlanService.add(salaryPlanAddDTO, tokenUser)
        );
    }


    @Override
    @PutMapping
    public Result<Integer> update(@Valid @RequestBody SalaryPlanUpdateDTO salaryPlanUpdateDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 修改工资方案权限校验

        int count = salaryPlanService.update(salaryPlanUpdateDTO, tokenUser);
        if (count == 0) {
            throw new ParamException("更新失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @PutMapping(value = "/update")
    public Result<Integer> updateForBusiness(@Valid @RequestBody SalaryPlanUpdateDTO salaryPlanUpdateDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 修改工资方案权限校验

        int count = salaryPlanService.update(salaryPlanUpdateDTO, tokenUser);
        if (count == 0) {
            throw new ParamException("更新失败，请刷新页面后重试");
        }
        return Result.success(count);
    }



    @Override
    @PostMapping(value = "/batch")
    public Result batch(@Valid @RequestBody SalaryPlanBatchDTO salaryPlanBatchDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 批量操作权限校验

        // 批量操作，注意校验，有越权风险
        // 新增
        List<SalaryPlanAddDTO> salaryPlanAddDTOList = salaryPlanBatchDTO.getSalaryPlanAddDTOList();
        if (!CollectionUtils.isEmpty(salaryPlanAddDTOList)) {
            salaryPlanService.batchSave(salaryPlanAddDTOList,tokenUser);
        }
        // 修改
        List<SalaryPlanUpdateDTO> salaryPlanUpdateDTOList = salaryPlanBatchDTO.getSalaryPlanUpdateDTOList();
        if (!CollectionUtils.isEmpty(salaryPlanUpdateDTOList)) {
            salaryPlanUpdateDTOList.parallelStream().forEach(
                    salaryPlanUpdateDTO -> {
                        salaryPlanService.update(salaryPlanUpdateDTO,tokenUser);
                    }
            );
        }
        // 删除
        List<String> deleteSalaryPlanIdList = salaryPlanBatchDTO.getDeleteSalaryPlanIdList();
        if (!CollectionUtils.isEmpty(deleteSalaryPlanIdList)) {
            salaryPlanService.deleteByIds(tokenUser,deleteSalaryPlanIdList.toArray(new String[0]));
        }
        return Result.success();
    }


    @Override
    @DeleteMapping(value = "/{salaryPlanId}")
    public Result<Integer> delete(@PathVariable String salaryPlanId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 彻底删除工资方案权限校验

        int count = salaryPlanService.deleteByIds(tokenUser, salaryPlanId);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @DeleteMapping
    public Result<Integer> deleteBatch(@RequestBody String[] salaryPlanIds) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 批量彻底删除工资方案权限校验

        int count = salaryPlanService.deleteByIds(tokenUser, salaryPlanIds);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @GetMapping(value = "/{salaryPlanId}")
    public Result<SalaryPlanVO> findById(@PathVariable String salaryPlanId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看工资方案权限校验
        return Result.success(
            salaryPlanService.selectById(salaryPlanId, tokenUser.getSiteId())
        );
    }


    @Override
    @GetMapping(value = "/listAll")
    public Result<List<SalaryPlanListVO>> listAll(@Valid SalaryPlanQuery salaryPlanQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看工资方案权限校验

        return Result.success(
            salaryPlanService.select(salaryPlanQuery, tokenUser.getSiteId())
        );
    }


    @Override
    @GetMapping
    public Result<Page<SalaryPlanListVO>> page(@Valid SalaryPlanQuery salaryPlanQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        return Result.success(
            salaryPlanService.selectPage(salaryPlanQuery, tokenUser.getSiteId())
        );
    }


}

